package com.example.fill;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Build;
import android.os.SystemClock;
import android.view.KeyEvent;

public class MusicUtils {
    /**
     * 播放—暂停
     */
    public static void playOrPauseMusic(Context context) {
        sendKeySync(context, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
        sendKeySync(context, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
    }

    /**
     * 上一曲
     */
    public static void lastMusic(Context context) {
        sendKeySync(context, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS);
        sendKeySync(context, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS);
    }

    /**
     * 下一曲
     */
    public static void nextMusic(Context context) {
        sendKeySync(context, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT);
        sendKeySync(context, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT);
    }

    private static void sendKeySync(Context context, int action, int code) {
        boolean dispatc = false;
        long eventtime = SystemClock.uptimeMillis();
        try {
            AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            if (am != null) {
                KeyEvent event;
                if (code == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                        if (am.isMusicActive()) {
                            event = new KeyEvent(eventtime, eventtime, action, KeyEvent.KEYCODE_MEDIA_PAUSE, 0);
                        } else {
                            event = new KeyEvent(eventtime, eventtime, action, KeyEvent.KEYCODE_MEDIA_PLAY, 0);
                        }
                    } else {
                        event = new KeyEvent(eventtime, eventtime, action, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);
                    }
                } else {
                    event = new KeyEvent(eventtime, eventtime, action, code, 0);
                }
                am.dispatchMediaKeyEvent(event);
                dispatc = true;
            }
        } catch (Exception ignored) {
        }
        if (!dispatc) {
            KeyEvent event = new KeyEvent(eventtime, eventtime, action, code, 0);
            Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
            downIntent.putExtra(Intent.EXTRA_KEY_EVENT, event);
            context.sendOrderedBroadcast(downIntent, null);
        }
    }

    public static void upVolume(Context context) {
        setStreamVolume(context, true);
    }

    public static void downVolume(Context context) {
        setStreamVolume(context, false);
    }

    private static void setStreamVolume(Context context, boolean add) {
        sendKeySync(context, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_VOLUME_UP);
        sendKeySync(context, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_VOLUME_UP);


        AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        if (am != null) {
            int currentVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);
            if (add) {
                currentVolume++;
                am.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FX_FOCUS_NAVIGATION_UP);//增加电量
            } else {
                currentVolume--;
                am.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FX_FOCUS_NAVIGATION_UP);//减少音量
            }
//            am.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, AudioManager.FLAG_SHOW_UI);
        }
    }

    /**
     * 调整音量
     */
    public static void adjustVolume(Context context, int curVolume) {
        AudioManager audioMgr = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        if (audioMgr == null) {
            return;
        }
        int maxVolume = audioMgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int volume = audioMgr.getStreamVolume(AudioManager.STREAM_MUSIC);
        int level = maxVolume * curVolume / 100;
        audioMgr.setStreamVolume(AudioManager.STREAM_MUSIC, level,
                AudioManager.FLAG_SHOW_UI);
        while (audioMgr.getStreamVolume(AudioManager.STREAM_MUSIC) != level) {
//            audioMgr.adjustVolume();
            audioMgr.adjustStreamVolume(AudioManager.STREAM_MUSIC, level > volume ? AudioManager.ADJUST_RAISE : AudioManager.ADJUST_LOWER
                    , AudioManager.FLAG_SHOW_UI);
        }
    }


    /**
     * 暂停
     */
    public static void pauseMusic(Context context) {
        sendKeySync(context, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE);
        sendKeySync(context, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PAUSE);
    }

    /**
     * 播放
     */
    public static void playMusic(Context context) {
        sendKeySync(context, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY);
        sendKeySync(context, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY);
    }
}
